#! /bin/bash

# 获取环境变量
PLAY_HOME=/opt/playground


function installSoft()
{
  #1.在frames.txt中查看是否需要安装soft
  sfInfo=`egrep "$1" $PLAY_HOME/frames.txt`

  sfName=`echo $sfInfo | cut -d " " -f1`
  isInstall=`echo $sfInfo | cut -d " " -f2`
  installNode=`echo $sfInfo | cut -d " " -f3`
  node=`hostname`

  #是否安装
  if [[ $isInstall = "true" && $installNode =~ $node ]];then

     #2.查看$PLAY_HOME/frames目录下是否有安装包
     sfIsExists=`find $PLAY_HOME/frames -name $sfName`
     if [[ ${#sfIsExists} -ne 0 ]];then

        if [[ ! -d /opt/app ]];then
           mkdir /opt/app && chmod -R 775 /opt/app
        fi

        #删除旧的
        sf_home_old=`find /opt/app -maxdepth 1 -name "*$1*"`
        for i in $sf_home_old;do
             rm -rf $i
        done

        #解压到指定文件夹/opt/app中
        echo "开始解压sf安装包"
        tar -zxvf $sfIsExists -C /opt/app >& /dev/null
        echo "sf安装包解压完毕"

        sf_home=`find /opt/app -maxdepth 1 -name "*$1*"`
        # 设置home
        if [ $2 -eq 1 ]; then
          sf_home_name=`echo "$1" | tr 'a-z' 'A-Z'`
          #配置 SF_HOME
          profile=/etc/profile.d/my_env.sh
          sudo sed -i "/^export ${sf_home_name}_HOME/d" $profile
          echo "export ${sf_home_name}=$sf_home" | sudo tee -a $profile

          #配置PATH
          sudo sed -i "/^export PATH=\$PATH:\$${sf_home_name}\/bin/d" $profile
          echo "export PATH=\$PATH:\$${sf_home_name}/bin" | sudo tee -a $profile

          #更新/etc/profile文件
          source /etc/profile && echo "更新/etc/profile环境变量完成"
        fi
        if [ $3 -eq 1 ]; then
          configurePrometheus $sf_home
        fi
        # 输出提示信息
        echo "-------------------------------"
        echo "|   $1在$node安装成功！  |"
        echo "-------------------------------"
     else
         echo "$PLAY_HOME/frames目录下没有$1安装包"
     fi
  else
      echo "$1不允许被安装在当前节点"
  fi
}

# 配置tez-site.xml文件
function configurePrometheus()
{
cat << EOF > $1/prometheus.yml
# my global config
global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]
        labels:
          app: "prometheus"
  - job_name: "pushgateway"
    static_configs:
      - targets: ["node102:9091"]
        labels:
          app: "pushgateway"
  - job_name: "node exporter"
    static_configs:
      - targets: ["node101:9100","node102:9100","node103:9100"]
        labels:
          app: "node exporter"
EOF

}


installSoft "grafana" 1 0
installSoft "prometheus" 1 1
installSoft "exporter" 1 0
installSoft "pushgateway" 1 0
installSoft "alertmanager" 1 0
